import type { StoreOptions } from "vuex";
export default {
    namespaced: true,
    state() {
        return {
            premission:[],
            username:''
        };
    },
    getters: {
        genMenus(state) {
            
            let newMenus: { [key:number]:{submenus?: {}[]} } = {};
            let premission = state.premission;
                
            for (let i = 0; i < premission.length; i++) {
                if (premission[i].parentId === 0) {
                    newMenus[premission[i].id] = {...premission[i] };
                } else {
                    //newMenus所在的对象(一级菜单)增加submenus属性
                    //1.根据二级菜单parentId获取一级菜单对象
                    let pid = premission[i].parentId;
                    newMenus[pid] = newMenus[pid] || {};
                    //2.往一级菜单对象上添加一个submenus属性(数组)
                    newMenus[pid].submenus = newMenus[pid].submenus || [];
                    // 3.把当前的二级菜单对象添加到submenus属性(数组)
                    newMenus[pid].submenus!.push(premission[i]);
                }
            }
            return newMenus;
        },
    },
    mutations: {
        upDatePre(state: { premission: any }, premission: any) {
            state.premission = premission;
        },
        username(state: { username: string }, name: string) {
            state.username = name;
        },
    },
    actions: {},
} as StoreOptions<userParam>;

